package network.okhttp.get7_2;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * 爬取百度首页的地址http://www.baidu.com并打印结果
 */
public class PageOperator {
    private OkHttpClient okHttpClient;
    public PageOperator(){
        okHttpClient = new OkHttpClient();
    }

    public String getPageContentSync(String url){
        if (StringUtils.isBlank(url)){
            return null;
        }
        String result = null;
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        try {
            result = call.execute().body().string();
            System.out.println("call"+ url +"content's size is" +result.length());
        } catch (IOException e) {
            System.out.println("requset" + url +" error!");
            e.printStackTrace();
        }
        return result;
    }
    public static void main(String[] args) {
        PageOperator pageOperator = new PageOperator();
        System.out.println( pageOperator.getPageContentSync("http://www.baidu.com"));
    }
}
